package com.ruoyi.web.controller.dm;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.DataRepeatException;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.AirCollectVO;
import com.ruoyi.dm.domain.po.ReanalysisQueryParam;
import com.ruoyi.dm.service.IReanalysisCollectRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 再分析资料数据
 */
@Slf4j
@RestController
@RequestMapping("/collect/reanalysis")
@Api(tags = "数据采集-再分析资料数据")
public class ReanalysisCollectController extends BaseController {

    @Autowired
    private IReanalysisCollectRecordService collectService;

    /**
     * 上传
     * @param files
     * @param fileType
     * @return
     */
    @PostMapping ("/upload")
    @ApiOperation("数据上传")
    @ApiImplicitParam(value = "上传文件",name = "files",dataTypeClass = File.class,required = true)
    public R<Boolean> collect(@RequestPart @RequestParam MultipartFile[] files,
                     @ApiParam(name ="dataType",value="资料类型") @RequestParam(value = "dataType", required = true)String dataType,
                     @ApiParam(name ="fileType",value="文件类型") @RequestParam(value = "fileType", required = true)String fileType) {
        try {
            List<String> res = collectService.uploadFile(files,dataType,fileType);
            if (res.size()>0) {
                return R.fail(CollectionUtil.join(res,",")+"重复上传,不再处理");
            }
            return R.ok();
        }catch (DataRepeatException e) {
            return R.fail("请勿重复上传");
        }
    }



    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("数据删除(单个)")
    @ApiImplicitParam(name ="id",value="id",type = "Long",required = true,paramType="query",dataTypeClass = Long.class)
    public R<Boolean>  del(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        return R.ok(collectService.delFile(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("/delBatch")
    @ApiOperation("数据删除(批量)")
    @ApiImplicitParam(name ="ids",value="ids",type = "Long[]",required = true,paramType="query",example = "1,2,3",dataTypeClass =Long[].class)
    public R<Boolean>  delBath(@ApiParam(value = "id集合", name = "ids") @RequestParam Long[] ids) {
        return R.ok(collectService.delBatch(ids));
    }

    /**
     * 检索
     * @param param
     * @return
     */
    @ApiOperation("数据查询")
    @PostMapping("/query")
    public R<IPage<AirCollectVO>> query(@ApiParam(name ="param",value="文件检索参数对象",required = true)@Validated @RequestBody ReanalysisQueryParam param) {
        IPage<AirCollectVO> query = collectService.query(param);
        return R.ok(query);
    }

    ///**
    // * 查看评估结果
    // * @param id
    // * @return
    // */
    //@GetMapping("/look")
    //@ApiOperation("查看评估结果")
    //@ApiImplicitParam(name ="id",value="id",type = "Long",required = true,paramType="query",dataTypeClass = Long.class,example = "1")
    //public R<CollectEstimateVO> queryRes(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
    //    try {
    //        CollectEstimateVO vo = collectService.lookRes( id);
    //        return R.ok(vo);
    //    } catch (Exception e) {
    //        String s = "文件尚未评估或评估异常";
    //        return R.fail(s);
    //    }
    //
    //}

    ///**
    // * 查看评估结果 详情
    // * @param id
    // * @return
    // */
    //@GetMapping("/lookDetail")
    //@ApiOperation("查看评估详情")
    //@ApiImplicitParam(name ="id",value="id",type = "Long",required = true,paramType="query",dataTypeClass = Long.class,example = "1")
    //public R<String> queryDetailRes(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
    //    String s = null;
    //    try {
    //        s = collectService.lookDetailRes( id);
    //        return R.ok(s);
    //    } catch (Exception e) {
    //        s = "文件尚未评估或评估异常";
    //        return R.fail(s);
    //    }
    //
    //}

    /**
     * 下载,返回文件路径
     * @param id
     * @return
     */
    @ApiOperation("数据下载(单个)")
    @ApiImplicitParam(name ="id",value="id",type = "Long",required = true,paramType="query",dataTypeClass = Long.class,example = "1")
    @GetMapping("/download")
    public void download( @ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id, HttpServletResponse response) {
        String s = collectService.download(id);
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }

    /**
     * 批量下载
     * @param ids
     * @return 文件压缩包路径
     */
    @ApiOperation("数据下载(批量)")
    @ApiImplicitParam(name ="ids",value="ids",type = "Long[]",required = true,paramType="query",example = "1,2",dataTypeClass = Long[].class)
    @GetMapping("/downloadBatch")
    public void  downloadBatch( @ApiParam(value = "文件类型", name = "ids") @RequestParam Long[] ids, HttpServletResponse response) {

        String s = collectService.downloadBatch(ids);
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }

    /**
     * 解析
     * @param id
     * @return
     */
    @ApiOperation("数据解析")
    @ApiImplicitParam(name ="id",value="id",type = "Long",required = true,paramType="query",dataTypeClass = Long.class,example = "1")
    @GetMapping("/analysis")
    public R<Boolean>  analysis( @ApiParam(value = "id", name = "id") @RequestParam Long id,
                                 @ApiParam(value = "解析方法", name = "method") @RequestParam(required = false) String method,
                                 @ApiParam(value = "经度", name = "lon") @RequestParam(required = false,defaultValue = "2.5") Double lon,
                                 @ApiParam(value = "纬度", name = "lat") @RequestParam(required = false,defaultValue = "2.5") Double lat,
                                 @ApiParam(value = "高程", name = "hight") @RequestParam(required = false,defaultValue = "2.5") Double hight
                                 ) {
        boolean res =  collectService.analysis(id,method,lon,lat,hight);
        return R.ok(res,"开始解析");
    }

    /**
     * 解析
     * @param ids
     * @return
     */
    @ApiOperation("批量解析")
    @GetMapping("/analysisBatch")
    public R<Boolean>  analysisBatch( @ApiParam(name ="ids 逗号间隔",value="ids",required = true,example = "1,2") @RequestParam String ids,
                                      @ApiParam(value = "经度", name = "lon") @RequestParam(required = false) Double lon,
                                      @ApiParam(value = "纬度", name = "lat") @RequestParam(required = false) Double lat,
                                      @ApiParam(value = "高程", name = "hight") @RequestParam(required = false) Double hight,
                                      @ApiParam(value = "解析方法", name = "method") @RequestParam(required = false) String method) {
        String[] split = ids.split(",");
        boolean res =  collectService.analysisBatch(split,method,lon,lat,hight);
        return R.ok(res,"开始解析");
    }
}
